import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.StringTokenizer;

/**
 * Example Map File:
 * 7
 * 5
 * _1 _1 _1 _1 _1 _1 _1
 * _1:texture=brick:opacity=0.5:shader=toon _0 _0 _a:model=a.obj _a:model=a.obj:collide=false _a:model=cat.obj _1
 * _1 _1 _1 _a:model=cd.obj _a:model=a.obj _a:model=a.obj:texture=squid.png:shader=toon _1
 * _1 _0 _0 _0 _0 _b _1
 * _1 _1 _1 _1 _1 _1 _1
 * 
 * Definitions:
 * 
 * If it starts with a dash in front of it, it is a built in type.
 * To use options, add a colon and attribute=foo
 * e.g. _1:texture=concrete.jpg:collision=false:opacity=0.5
 * 
 * 0 - Empty Space
 * 1 - Standard Block
 * 	Options:
 * 		texture=filename
 * 		collision=choice
 * 		opacity=float (Between 0 and 1)
 * 		shader=program
 * a - Load Model
 * 	Options:
 * 		model=filename
 * 		texture=filename
 * 		shader=program
 * b - spawn point
 * c - half block (used for jumping, same options as standard block as well as some others)
 * 	Options:
 * 		bottom=choice (Whether it shows up on the top or bottom)
 * d - Ball, which you can grab for points
 * e - Enemy
 * 	Options:
 * 		id=num
 * 		step=stepNum
 * 
 * Using "|" will combine blocks in the same position
 * e.g. _1:collide=false:opacity=.001|_b:model=cat.obj
 * _0 is assumed
 */

public class MapImporter
{
	private Object[][] blockList = null;

	public MapImporter(String fileName)
	{
		BlockType blockHandler = new BlockType();
		Scanner scan = null;
		try
		{
			scan = new Scanner(new File(fileName));
		}
		catch (FileNotFoundException e)
		{
			e.printStackTrace();
		}
		int numX = Integer.parseInt(scan.nextLine());
		int numY = Integer.parseInt(scan.nextLine());

		blockList = new Object[numY][numX];

		for(int i = 0; i < numY; i++)
		{
			StringTokenizer curLine = new StringTokenizer(scan.nextLine());
			for(int j = 0; j < numX; j++)
			{
				String curToken = curLine.nextToken();
				if(curToken.startsWith("_")) //Built in static block
				{
					if(curToken.startsWith("_0"))
					{
						blockList[i][j] = blockHandler.new EmptyBlock();
					}
					else if(curToken.startsWith("_1"))
					{
						blockList[i][j] = blockHandler.new StandardBlock();
					}
					else if(curToken.startsWith("_b"))
					{
						blockList[i][j] = blockHandler.new SpawnBlock();
					}
					else if(curToken.startsWith("_c"))
					{
						blockList[i][j] = blockHandler.new HalfBlock();
					}
					else if(curToken.startsWith("_d"))
					{
						blockList[i][j] = blockHandler.new BallFlag();
					}
				}
			}
		}
	}
	
	public Object getObject(int x, int y)
	{
		return blockList[x][y];
	}
	
	public int sizeX()
	{
		return blockList.length;
	}
	
	public int sizeY()
	{
		return blockList[0].length;
	}
}